<?php
/**
 * Controller is an instance of CController (system.web). 
 *
 * CController manages a set of actions which deal with the corresponding user requests. 
 * Through the actions, CController coordinates the data flow between models and views. 
 *
 */
class Controller extends CController {
	public $menu=array();
	public $breadcrumbs=array();

	function init()
    	{
        parent::init();
        
	$app = Yii::app();
		if(!isset(Yii::app()->request->cookies['tracer'])){
			$model = new Visit;
			$model->ip_adr = $_SERVER['REMOTE_ADDR'];
			$model->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
			$model->country = $model->get_country();
			$model->client = $_SERVER['HTTP_USER_AGENT'];
			$model->visit_date = date('Y-m-d');
			$model->time = date("H:i:s");  
			$model->save();
			$cookie = new CHttpCookie('tracer',$model->time);
			$cookie->expire = 0; 
			Yii::app()->request->cookies['tracer']=$cookie;
		}
		
		if (isset($app->session['lang'])){
		    $app->language = $app->session['lang'];
		}

    	}

	
	public function filterAccessControl($filterChain) 
	{
		$filter=new AccessControlFilter;
		$filter->setRules($this->accessRules());
		$filter->filter($filterChain);
	}
	
	public function render($view,$data=null,$return=false,$processOutput=false)
	{
		if($_SERVER['HTTP_X_REQUESTED_WITH'] ? $_SERVER['HTTP_X_REQUESTED_WITH']==='XMLHttpRequest' : false){
			
			
			parent::renderPartial($view,$data,$return,$processOutput);
			$this->widget('zii.widgets.CBreadcrumbs', array('links'=>$this->breadcrumbs,'homeLink'=>'<a href="/">'.Yii::t('yii','Home').'</a>'));
			if (!empty($this->menu)) {
			$this->widget('Menu',array('items'=>$this->menu));
			}
		}else{			
			parent::render($view,$data,$return);
		}
	}

	public function noAjaxRender($view,$data=null,$return=false)
	{
		parent::renderPartial($view,$data,$return);
	}
	
}
